package com.example.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.InterviewArrangeMapper;
import com.example.pojo.InterviewArrange;
import com.example.pojo.ResumeClassify;
import com.example.service.IInterviewArrangeService;
import com.example.util.DateUtils;
import com.example.vo.MailVo;
import com.example.vo.ResultVo;
import com.example.vo.per.IASendEmailParams;
import com.example.vo.respvo.IHandleSendOffice;
import com.example.vo.respvo.InterviewHandle;
import com.example.vo.respvo.Option;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.System.err;

/**
 * <p>
 * 面试安排表 服务实现类
 * </p>
 *
 * @author wsl
 * @since 2022-04-15
 */
@Service
public class InterviewArrangeServiceImpl extends ServiceImpl<InterviewArrangeMapper, InterviewArrange> implements IInterviewArrangeService {

    @Autowired
    private InterviewArrangeMapper iam;
    @Autowired
    private SendEmailServiceImpl sesi;
    @Autowired
    private ResumeClassifyServiceImpl rcs;
    @Autowired
    private LabourContractServiceImpl lcsi;

    @Override
    public PageInfo<InterviewArrange> getInterviewArrangePageInfo(Integer hr, String position,Integer interCycle, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<InterviewArrange> list = baseMapper.selectInterviewArrangePageInfo(hr,position,interCycle);
        list.forEach(o->{
            Integer time = DateUtils.getCompareTime(String.valueOf(o.getInterviewDate()) + String.valueOf(o.getInterviewTime()));
            if (time > 0 && (o.getResult() == 2 || o.getResult() == null)){
                uptByiaid(o.getId());
                delinterviewRecordAndResume(o.getResumeClassifyId(),o.getId());
            }
        });
        uptByiaid(1);
        return new PageInfo<>(list);
    }

    @Override
    public void uptByiaid(Integer id) {
        InterviewArrange arrange = new InterviewArrange();
        arrange.setId(id);
        arrange.setStatus(4);
        arrange.setResult(2);
        arrange.setRemark("面试迟到！");
        iam.updateById(arrange);
    }

    @Override
    public ResultVo getHrList() {
        List<Option> hr = baseMapper.selectHrList();
        return ResultVo.response(hr);
    }

    @Override
    public ResultVo getPositionOptions() {
        List<Object> list = baseMapper.selectObjs(
                new QueryWrapper<InterviewArrange>()
                        .select("distinct position")
        );
        return ResultVo.response(list);
    }

    @Override
    public PageInfo<InterviewHandle> getInterviewHandlePageInfo(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<InterviewHandle> list = baseMapper.selectInterviewHandleList();
        return new PageInfo<>(list);
    }

    @Override
    @Transactional
    public void sendEmailByE(List<IASendEmailParams> isp) {
        for (IASendEmailParams params : isp) {
            new Thread(()->{
                MailVo mailVo = new MailVo();
                mailVo.setId("1001");
                mailVo.setFrom("3507641746@qq.com");
                mailVo.setSubject("面试通知");
                mailVo.setTo(params.getResume().getEmail());
                Map<String,Object> map = new HashMap<>();
                map.put("gw",params.getPosition());
                map.put("sj",params.getInterviewDate() + "   " + params.getInterviewTime());
                map.put("jd",params.getPhaseName());
                map.put("xs",params.getFormName());
                mailVo.setAttachment(map);
                sesi.sendTemplateMail(mailVo,"");
            }).start();
            InterviewArrange arrange = new InterviewArrange();
            arrange.setId(params.getId());
            arrange.setStatus(2);
            iam.updateById(arrange);
        }

    }

    @Override
    @Transactional
    public Boolean sendEmailByIam(IHandleSendOffice ihso) {
        new Thread(()->{
            MailVo mailVo = new MailVo();
            mailVo.setId("1001");
            mailVo.setFrom("3507641746@qq.com");
            mailVo.setSubject("聘用通知");
            mailVo.setTo(ihso.getMailIds());
            Map<String,Object> map = JSON.parseObject(JSON.toJSONString(ihso), Map.class);
            mailVo.setAttachment(map);
            sesi.sendTemplateMail(mailVo,"聘用");
        }).start();
        ResumeClassify classify = new ResumeClassify();
        classify.setId(ihso.getRcId());
        classify.setIsHire(2);
        boolean b = rcs.updateById(classify);

        if(b){
            Integer integer = lcsi.insertLc(ihso);
            if (integer != 0 ){
                return true;
            }
        }
        return false;
    }

    @Override
    public Boolean delinterviewRecordAndResume(Integer rcid, Integer iaid) {
        ResumeClassify r = new ResumeClassify();
        r.setIsHire(0);
        r.setDeleted(1);
        r.setMatchDegree(0);
        r.setId(rcid);
        boolean b = rcs.updateById(r);
        if (b){
            InterviewArrange i = new InterviewArrange();
            i.setId(iaid);
            i.setDeleted(1);
            int i1 = iam.updateById(i);
            if (i1 != 0){
                return true;
            }
        }
        return false;
    }


}
